package Test;

import JaveBean.Manage;
import dao.manageDao;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Properties;

/**
 * @Auther:Liu
 * @Testname:TestMybaties3
 * @Date:2025/3/26 16:55
 */
public class TestMybaties3 {
    /**
     * 加载日志
     *
     * @throws Exception
     */
    public void RZ() throws Exception {
        //输出日志信息
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }

    /**
     * 获取工厂对象
     *
     * @return
     * @throws Exception
     */
    public SqlSession GC() throws Exception {
        //1.读取mybatis配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("config/Mybatiesconfig1.xml");
        //2.读取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //3.通过工厂获取对象
        SqlSession sqlSession = build.openSession();
        return sqlSession;
    }

    /**
     * 查找数据
     * @throws Exception
     */
    @Test
    public void findAll() throws Exception {
        RZ();
        SqlSession gc = GC();
        manageDao mapper = gc.getMapper(manageDao.class);
        List<Manage> all = mapper.findAll();
        System.out.println(all);
    }

    /**
     * 删除一条数据
     * @throws Exception
     */
    @Test
    public void delete()throws Exception{
        RZ();
        SqlSession gc = GC();
        manageDao mapper = gc.getMapper(manageDao.class);
        mapper.delete(26);
        gc.commit();
    }

    /**
     * 插入数据
     * @throws Exception
     */
    @Test
    public void save()throws Exception{
        RZ();
        SqlSession gc = GC();
        manageDao mapper = gc.getMapper(manageDao.class);
        Manage manage = new Manage("打败","1232",null);
        mapper.sava(manage);
        gc.commit();
    }

    /**
     * 查找一条数据
     * @throws Exception
     */
    @Test
    public void findbyid() throws Exception {
        SqlSession gc = GC();
        manageDao mapper = gc.getMapper(manageDao.class);
        List<Manage> findbyid = mapper.findbyid(2);
        System.out.println(findbyid);
    }
}
